import datetime
import json

from celery import shared_task
from django.conf import settings

from app.models import get_user_mobile_list, UserAddress, WxUser
from vip.models import get_remind_user_list


def remind_user_buy_1():
    """
    活动开始提醒
    """
    from jiaozi.utils import WeChatMsg

    weekday = datetime.date.today().weekday()
    days = 1 - weekday
    thursday = datetime.date.today() + datetime.timedelta(days=days)
    time2 = '{} 19:00:00'.format(thursday.strftime('%Y-%m-%d'))

    template = {
        "touser": "",
        "template_id": "0oiRLJW8dV9X-Kz5xAiFulsFaUqf5Db0C-dWHmA3S9Y",
        "data": {
            "thing1": {
                "value": "酒厂模拟器",
            },
            "time2": {
                "value": time2,
            },
            "thing3": {
                "value": "本周选酒快开始了，请活动手指准备开抢！",
            },
        },
        # "miniprogram": {
        #     "appid": settings.WX_APP_ID,
        #     "pagepath": "find/index"
        # },
    }

    wx = WeChatMsg(template=template)
    user_list = get_remind_user_list()
    wx.user_list = user_list
    wx.post_data()


def remind_user_buy_2():
    """
    活动即将截止提醒
    """
    from product.views import get_user_can_buy_amount
    from jiaozi.utils import WeChatMsg

    weekday = datetime.date.today().weekday()
    days = 3 - weekday
    thursday = datetime.date.today() + datetime.timedelta(days=days)
    date4 = '{} 19:00:00'.format(thursday.strftime('%Y-%m-%d'))

    template = {
        "touser": "",
        "template_id": "fmO6xXBe9hxQDaqxvK8RvBfInOZlb0NtS2mk1gJ5VjY",
        "data": {
            "thing1": {
                "value": "酒厂模拟器",
            },
            "date4": {
                "value": date4,
            },
            "thing5": {
                "value": "本周兑换即将结束，请尽快去兑换，以免错过!",
            },
        },
        # "miniprogram": {
        #     "appid": settings.WX_APP_ID,
        #     "pagepath": "find/index"
        # },
    }

    wx = WeChatMsg(template=template)
    user_list = get_remind_user_list()
    wx.user_list = user_list
    wx.post_data()


def remind_user_buy_1m():
    """
    短信提醒用户，开售提醒
    """
    from jiaozi.sms_aliyun import aliyun_send_sms
    user_list = get_remind_user_list()
    user_mobile_list = get_user_mobile_list(user_list)
    # para = {"time": "19:00"}
    for mobile in user_mobile_list:
        aliyun_send_sms(mobile=mobile, para='', template_code='SMS_218727528')


@shared_task
def remind_user_buy_2m():
    """
    短信截止售卖提醒
    """
    from jiaozi.sms_aliyun import aliyun_send_sms
    user_list = get_remind_user_list()
    user_mobile_dict = dict(WxUser.objects.filter(
        pk__in=user_list
    ).values_list('id', 'mobile').order_by('pk'))
    user_name_dict = dict(UserAddress.objects.filter(
        user_id__in=user_list
    ).values_list('user_id', 'name').order_by('pk'))
    res = list()
    for user_id, mobile in user_mobile_dict.items():
        para = {"name": user_name_dict.get(user_id), "week": "四", "ctime": "19:00"}
        res.append({'user_id': user_id, 'mobile': mobile, 'para': para})
        aliyun_send_sms(mobile=mobile, para=json.dumps(para), template_code='SMS_219405681')
    return res


def remind_user_buy_test():
    from jiaozi.sms_aliyun import aliyun_send_sms
    user_mobile_list = settings.TEST_MOBILE
    for mobile in user_mobile_list:
        para = {"name": 'name', "week": "四", "ctime": "19:00"}
        aliyun_send_sms(mobile=mobile, para=json.dumps(para), template_code=settings.SMS_TEMPLATE_2)
